package code;
import models.ListNode;
import models.RandomListNode;
public class Clone {
    public RandomListNode Solution(RandomListNode pHead) {
        if(pHead==null){
            return null;
        }
        RandomListNode head = pHead;
        while(head!=null){
            RandomListNode n = new RandomListNode(head.val);
            n.next = head.next;
            head.next = n;
            head = head.next.next;
        }
        head = pHead;
        while (head!=null){
            if(head.random!=null){
                head.next.random = head.random.next;
            }
            head = head.next.next;
        }
        head = pHead;
        RandomListNode cur = head.next;
        RandomListNode re = head;
        while (head!=null){
            head.next=head.next.next;
            if(cur.next.next!=null){
                cur.next=cur.next.next;
            }
            head=head.next;
            cur=cur.next;
        }
        return re;
    }
}
